package com.asktgapp.user.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.health.PidHealthStats;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.asktgapp.architecture.retrofit.ApiException;
import com.asktgapp.architecture.retrofit.ApiResponseBody;
import com.asktgapp.JGTool.Utils;
import com.asktgapp.R;
import com.asktgapp.application.ApiUtil;
import com.asktgapp.application.Apiservice;
import com.asktgapp.architecture.rxjava.ExceptionHandle;
import com.asktgapp.base.BaseFragment;
import com.asktgapp.model.IndustryInfoVO;
import com.asktgapp.user.activity.IndustryInfoDetailActivity;
import com.asktgapp.user.activity.PublishCommonSenseActivity;
import com.asktgapp.user.activity.PublishUsedMachineActivity;
import com.asktgapp.utils.ImageDisplayUtil;
import com.asktgapp.widget.RefreshRecyclerView;
import com.asktgapp.modulebase.common.adapter.BaseAdapter;
import com.asktgapp.modulebase.common.adapter.BaseViewHolder;
import com.asktgapp.modulebase.common.util.PreferencesUtil;

import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * time:2018/1/24
 * Create By ZhaoKai
 * Email: dczk@foxmail.com
 * Description:
 */

public class IndustryInformationFragment extends BaseFragment {
    private RefreshRecyclerView mRefreshRecyclerView;
    private RecyclerView mRecyclerView;
    private BaseAdapter mBaseAdapter;
    private int mPage = 1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_my_collection, container, false);
        return super.onCreateView(inflater, viewGroup, savedInstanceState);
    }

    @Override
    protected void initView(View rootView) {
        setHasOptionsMenu(true);
        getBaseActivity().setBackNavigationIcon();
        mRefreshRecyclerView = rootView.findViewById(R.id.recycle);
        mRefreshRecyclerView.setRefreshAction(new RefreshRecyclerView.Action() {
            @Override
            public void onAction(int page) {
                mPage = page;
                getData(mPage);
            }
        });
        mRefreshRecyclerView.setLoadMoreAction(new RefreshRecyclerView.Action() {
            @Override
            public void onAction(int page) {
                mPage = page;
                getData(mPage);
            }
        });
        mRecyclerView = mRefreshRecyclerView.getRecyclerView();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        mBaseAdapter = new BaseAdapter(getActivity()) {

            @Override
            public int getItemViewType(int position) {
                IndustryInfoVO bean = (IndustryInfoVO) mBaseAdapter.getData().get(position);
                if (bean.getBannerList().size() <= 1) {
                    return 10;
                } else if (bean.getBannerList().size() == 2) {
                    return 20;
                } else
                    return 30;
            }

            @Override
            public BaseViewHolder getViewHolder(ViewGroup parent, int viewType) {
                BaseViewHolder holder;

                if (viewType == 10) {
                    holder = new BaseViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.item_industry_information, parent, false), new BaseViewHolder.Callbacks() {
                        @Override
                        public void bind(BaseViewHolder holder, Object data) {
                            ImageView img = holder.getView(R.id.iv_industry_img);
                            TextView title = holder.getView(R.id.tv_industry_title);
                            TextView time = holder.getView(R.id.tv_industry_time);
                            TextView comment = holder.getView(R.id.tv_industry_read_number);
                            TextView like = holder.getView(R.id.tv_industry_like);
                            if (data instanceof IndustryInfoVO) {
                                IndustryInfoVO bean = (IndustryInfoVO) data;
                                if(bean.getBannerList().size()==1){
                                    ImageDisplayUtil.LoadNetImage(img, getActivity(), bean.getBannerList().get(0));
                                }
                                title.setText(bean.getTitle());
                                time.setText(Utils.getDateMD(bean.getAdd_time()));
                                comment.setText(String.valueOf(bean.getComment_number()));
                                like.setText(String.valueOf(bean.getLike_number()));
                                holder.setText(R.id.lookNumber, bean.getViewNum());
                            }
                        }

                        @Override
                        public void onItemClick(View view, int position) {
                            IndustryInfoVO bean = (IndustryInfoVO) mBaseAdapter.getData().get(position);
                            Intent intent = new Intent(getActivity(), IndustryInfoDetailActivity.class);
                            intent.putExtra("id", bean.getId());
                            startActivity(intent);
                        }
                    });
                } else if (viewType == 20) {
                    holder = new BaseViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.item_industry_information_two_image, parent, false), new BaseViewHolder.Callbacks() {
                        @Override
                        public void bind(BaseViewHolder holder, Object data) {

                            TextView title = holder.getView(R.id.tv_industry_title);
                            TextView time = holder.getView(R.id.tv_industry_time);
                            TextView comment = holder.getView(R.id.tv_industry_read_number);
                            TextView like = holder.getView(R.id.tv_industry_like);
                            ImageView ivQuestionImg1 = holder.getView(R.id.ivQuestionImg1);
                            ImageView ivQuestionImg2 = holder.getView(R.id.ivQuestionImg2);
                            if (data instanceof IndustryInfoVO) {

                                IndustryInfoVO bean = (IndustryInfoVO) data;

                                ImageDisplayUtil.LoadNetImage(ivQuestionImg1, getActivity(), bean.getBannerList().get(0));
                                ImageDisplayUtil.LoadNetImage(ivQuestionImg2, getActivity(), bean.getBannerList().get(1));


                                title.setText(bean.getTitle());
                                time.setText(Utils.getDateMD(bean.getAdd_time()));
                                comment.setText(String.valueOf(bean.getComment_number()));
                                like.setText(String.valueOf(bean.getLike_number()));
                                holder.setText(R.id.lookNumber, bean.getViewNum());
                            }
                        }

                        @Override
                        public void onItemClick(View view, int position) {
                            IndustryInfoVO bean = (IndustryInfoVO) mBaseAdapter.getData().get(position);
                            Intent intent = new Intent(getActivity(), IndustryInfoDetailActivity.class);
                            intent.putExtra("id", bean.getId());
                            startActivity(intent);
                        }
                    });
                } else {
                    holder = new BaseViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.item_industry_information_three_image, parent, false), new BaseViewHolder.Callbacks() {
                        @Override
                        public void bind(BaseViewHolder holder, Object data) {

                            TextView title = holder.getView(R.id.tv_industry_title);
                            TextView time = holder.getView(R.id.tv_industry_time);
                            TextView comment = holder.getView(R.id.tv_industry_read_number);
                            TextView like = holder.getView(R.id.tv_industry_like);
                            ImageView ivQuestionImg1 = holder.getView(R.id.ivQuestionImg1);
                            ImageView ivQuestionImg2 = holder.getView(R.id.ivQuestionImg2);
                            ImageView ivQuestionImg3 = holder.getView(R.id.ivQuestionImg3);
                            if (data instanceof IndustryInfoVO) {

                                IndustryInfoVO bean = (IndustryInfoVO) data;

                                ImageDisplayUtil.LoadNetImage(ivQuestionImg1, getActivity(), bean.getBannerList().get(0));
                                ImageDisplayUtil.LoadNetImage(ivQuestionImg2, getActivity(), bean.getBannerList().get(1));
                                ImageDisplayUtil.LoadNetImage(ivQuestionImg3, getActivity(), bean.getBannerList().get(2));


                                title.setText(bean.getTitle());
                                time.setText(Utils.getDateMD(bean.getAdd_time()));
                                comment.setText(String.valueOf(bean.getComment_number()));
                                like.setText(String.valueOf(bean.getLike_number()));
                                holder.setText(R.id.lookNumber, bean.getViewNum());
                            }
                        }

                        @Override
                        public void onItemClick(View view, int position) {
                            IndustryInfoVO bean = (IndustryInfoVO) mBaseAdapter.getData().get(position);
                            Intent intent = new Intent(getActivity(), IndustryInfoDetailActivity.class);
                            intent.putExtra("id", bean.getId());
                            startActivity(intent);
                        }
                    });
                }
                return holder;
            }
        };
        mRecyclerView.setAdapter(mBaseAdapter);
    }


    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.finish, menu);
        menu.getItem(0).setTitle("发布");
        if (!PreferencesUtil.getBoolean(getActivity(), PreferencesUtil.KEY_IS_ENGINEER))
            menu.getItem(0).setVisible(false);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (checkIsLogin("登录后可体验更多内容"))
            startActivity(new Intent(getActivity(), PublishCommonSenseActivity.class));
        return super.onOptionsItemSelected(item);
    }


    @Override
    public void loadData() {
        visibleLoading();
        getData(mPage);
    }

    public void getData(final int mPage) {

        HashMap params = new HashMap();
        params.put("userid", PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID));
        params.put("page", mPage);
        Apiservice apiservice = ApiUtil.getInstance().create();
        Call<ApiResponseBody<List<IndustryInfoVO>>> call = apiservice.findInfoList(params);
        call.enqueue(new Callback<ApiResponseBody<List<IndustryInfoVO>>>() {
            @Override
            public void onResponse(Call<ApiResponseBody<List<IndustryInfoVO>>> call, Response<ApiResponseBody<List<IndustryInfoVO>>> response) {
                inVisibleLoading();
                mRefreshRecyclerView.endPage();
                if (response.isSuccessful()) {
                    List<IndustryInfoVO> list = response.body().getResult();
                    if (mPage == 1) {
                        if (list.size() == 0) {
                            visibleNoData();
                        } else {
                            mBaseAdapter.setData(list);
                            mRefreshRecyclerView.showNextMore(mPage);
                        }
                    } else {
                        if (list.size() == 0) {
                            mRefreshRecyclerView.showNoMore();
                        } else {
                            mBaseAdapter.addData(BaseAdapter.AddType.LASE, list);
                            mRefreshRecyclerView.showNextMore(mPage);
                        }
                    }
                } else {
                    showTost(response.raw().message(), 0);
                }
            }

            @Override
            public void onFailure(Call<ApiResponseBody<List<IndustryInfoVO>>> call, Throwable t) {
                inVisibleLoading();
                mRefreshRecyclerView.endPage();
                ApiException apiException = ExceptionHandle.handleException(t);
                if (apiException.isTokenInvalid()) {
                    showUnLoginSnackbar();
                } else if (apiException.isNetConnected()) {
                    showSetNetworkSnackbar();
                } else {
                    showTost(apiException.getMessage(), 1);
                }
            }
        });
    }
}
